// pages/Census/data-detail/index.js
const Controllers = require('../../controllers/ControllerMy.js').controller;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    money: '',
    min: 0,
    max: 0,
    balance: 0,
    bankData: {}
  },
  getInput(e) {
    var that = this;
    var name = e.target.dataset.name;
    var value = e.detail.value;
    console.log('value==', value);
    console.log('isNaN(value)==', isNaN(value));
    
    if (isNaN(value)) {
      return;
    }
    that.setData({
      money: value
    })
  },
  selectWithdraw() {
    var that = this;
    Controllers.selectWithdraw({type: 3}).then(res => {
      console.log('selectWithdraw--res==', res);
      const { code, data, desc } = res;
      const { min, max, balance } = data;
      that.setData({
        min: parseInt(min),
        max: parseInt(max),
        balance,
        money: that.data.money || balance,
        selectWithdrawData: data
      })
    })
  },
  getAgentBank() {
    var that = this;
    wx.showLoading();
    Controllers.getAgentBank({}).then(res => {
      console.log('getAgentBank--res==', res);
      const { code, data, desc } = res;
      that.setData({
        bankData: data
      });
      wx.hideLoading();
    })
  },
  changeMoney(e) {
    var that = this;
    var min = that.data.min;
    var max = that.data.max;
    var balance = that.data.balance;
    that.setData({
      money: balance > max ? max : balance
    })
  },
  agentWithdraw() {
    var that = this;
    var bankData = that.data.bankData;
    var min = that.data.min;
    var max = that.data.max;
    var balance = that.data.balance;
    
    var money = that.data.money;
    var showToast = false;
    var title = '';
    if (!money) {
      showToast = true;
      title = '暂无提现金额'
    }
    // if (isNaN(money) || money <= 0) {
    //   showToast = true;
    //   title = '请输入正确的金额';
    // } else if ( money > balance) {
    //   showToast = true;
    //   title = '请输入正确的金额';
    // }
    //  else if (bankData && bankData.bankAccountId) {
    //   showToast = true;
    //   title = '请选择提现银行卡';
    // }
    if (showToast) {
      wx.showToast({
        title,
        icon: 'none'
      });
      return;
    }
    if (that.data.isLoading) {
      return;
    }
    that.data.isLoading = true;
    wx.showLoading();
    const params = {
      money,
      // bankAccountId: bankData.id
    }
    Controllers.agentWithdraw(params).then(res => {
      console.log('agentWithdraw--res==', res);
      const { code, data, desc } = res;
      wx.hideLoading()
      wx.showToast({
        title: desc,
        icon: code === 1 ? 'success' : 'none'
      });
      if (code === 1) {
        setTimeout(() => {
          that.data.isLoading = false;
          wx.navigateBack();
        }, 1000);
      } else {
        that.data.isLoading = false;
      }
    }).catch(err => {
      that.data.isLoading = false;
    })
  },
  withdrawAll() {
    var that = this;
    if (that.data.isLoading) {
      return;
    }
    that.data.isLoading = true;
    Controllers.withdrawAll({}).then(res => {
      console.log('withdrawAll--res==', res);
      const { code, data, desc } = res;
      wx.hideLoading()
      wx.showToast({
        title: desc,
        icon: code === 1 ? 'success' : 'none'
      });
      if (code === 1) {
        setTimeout(() => {
          that.data.isLoading = false;
          wx.navigateBack();
        }, 1000);
      } else {
        that.data.isLoading = false;
      }
    }).catch(err => {
      that.data.isLoading = false;
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.selectWithdraw();
    // that.getAgentBank();
  },
  changeTab(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.setData({
      active: index
    })
  },
  openPage(e) {
    var url = e.currentTarget.dataset.url;
    if (url) {
      app.openPage({ url });
    } else {
      wx.showToast({
        title: '未获取页面路径',
        icon: 'none'
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})